<%@include file="/header.jsp"%>
<%@include file="/login/checkLogin.jsp"%>
<%@ page language="java" contentType="text/html" %>
<%@ page import="dataGateway.ItemTable"%>
<%@ page import="domainLogic.Item" %>
<%@ page import="domainLogic.Category" %>
<%@ page import="java.sql.Timestamp" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%
	if (request.getMethod().equals("POST"))
{
%>
	<jsp:useBean id="newItem" class="domainLogic.Item" scope="request" />
	<jsp:setProperty name="newItem" property="*"/>
	<%
		String date = (String)request.getParameter("date");
		String time = (String)request.getParameter("time");

		// Chceck date
		if(date == null || time == null)
		{
			out.println(common.JspUtil.makeErrorMsg("Please input date and time!"));
			return;
		}
		
		SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy+HH:mm");
		Date inputdate = null;
		try {
			inputdate = formatter.parse(date+"+"+time);
		}
		catch (java.text.ParseException e) {
			out.println(common.JspUtil.makeErrorMsg("Please input date and time correctly!"));
			return;
		}
		newItem.setEnddate(new Timestamp(inputdate.getTime()));
		newItem.setRealenddate(newItem.getEnddate());
		newItem.setUserId(common.JspUtil.getLoggedInUserId(session));
		
		if(newItem.validate(out) == false)
		{
			return;
		}
		
		session.setAttribute("newItem",newItem);
	%>
<form method="post" action="addItemProcess.jsp">
<h3>Item details</h3>
<p>Please check your details!</p>
<table border="2" cellpadding="3" cellspacing="0" style="border-collapse:collapse;" rules="rows" frame="hsides">
	<tr><td colspan="2"></td></tr>
	<tr><td></td>
		<td> <input  type="submit" value="Register"><input  type="button" value="Modify" onClick="history.go(-1)"></td>
	</tr>
	<tr>
		<td>user</td>
		<td><%=newItem.getUserId()%></td>
	</tr>
	<tr>
		<td>category</td>
		<td><%=newItem.getCatId()%></td>
	</tr>
	<tr>
		<td>Item name</td>
		<td><%=newItem.getName()%></td>
	</tr>
	<tr>
		<td>Description</td>
		<td><%=newItem.getDescription()%></td>
	</tr>
	<tr>
		<td>Starting Price </td>
		<td><%=newItem.getStartingprice()%></td>
	</tr>
	<tr>
		<td>Reserve Price</td>
		<td><%=newItem.getReserveprice()%></td>
	</tr>
	<tr>
		<td>Buyout Price</td>
		<td><%=newItem.getBuyoutprice()%></td>
	</tr>
	<tr>
		<td>End time</td>
		<td><%=newItem.getEnddate()%></td>
	</tr>
</table>
</form>
<%
	}
else
{
%>
<html>
<head><title>Create new auction item.</title>
<script type="text/javascript" src="<%=common.JspUtil.URL_CONTENT_PAGE%>/js/itemCheck.js"></script>
</head></head>

<body>
<form name="frm" method="post" >
<table bgcolor="#f2dfa9" border="1" align="center">
	<tr>
		<td  colspan="2" align="center">
			<font size="3" color="green"><b>Details</b></font>
		</td>
	</tr>
	<tr>
		<th>user</th>
		<td><%=common.JspUtil.getLoggedInUserName(session)%></td>
	</tr>
	<tr>
		<th>category</th>
		<td>
<select name="catId">
<%
	java.util.List<Category> categories = dataGateway.CategoryTable.getInstance().getCategoriesAll();
for(Category cat : categories)
{
%>
	  <option value="<%=cat.getId() %>"><%=cat.getName()%></option>
<%
}
%>
</select>		
		</td>
	</tr>
	<tr>
		<th> Item name </th>
		<td><input type="text" name="name"></td>
	</tr>
	<tr>
		<th> Description </th>
		<td><textarea name="description" rows="5" cols="50"></textarea></td>
	</tr>
	<tr>
		<th> Starting Price&nbsp;<font size="2" color="Grey">(Optional)</font></th>
		<td><input type="text" name="startingprice"> </td>
	</tr>
	<tr>
		<th> Reserve Price&nbsp;<font size="2" color="Grey">(Optional)</font></th>
		<td><input type="text" name="reserveprice"></td>
	</tr>
	<tr>
		<th> Buyout Price&nbsp;<font size="2" color="Grey">(Optional)</font></th>
		<td><input type="text" name="buyoutprice"></td>
	</tr>
	<tr>
		<th> End date </th>
		<td>DD/MM/YYYY : <input type="text" name="date"></td>
	</tr>
	<tr>
		<th> End time </th>
		<td>HH:MM<input type="text" name="time"></td>
	</tr>
	<tr>
		<td colspan="2"><center>
			<input type="button" value="Register" onClick="checkForm()">&nbsp;&nbsp;
			<input type="reset" value="Cancel"></center></td>
	</tr>
</table>
</form>
</body>
</html>
<% 
} %>
<%@include file="/footer.jsp"%>